//
//  ShareEditController.m
//  NeteaseLottery
//
//  Created by wangbo on 11-7-14.
//  Copyright 2011 netease. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "ShareEditController.h"
#import "UIFactory.h"
#import "ShareError.h"

#define MAXWORDS 150

@interface ShareEditController()
-(void)refreshWithAcctount;
@end

@implementation ShareEditController

@synthesize attachImage=_attachImage;
@synthesize shareType=_shareType;

-(id)initWithEngine:(ShareEngine*)engine {
    self =[super init];
    if (self) {
        _engine =[engine retain];
        _engine.delegate = self;
    	_engine.baseViewController = self;
    }
    return self;
}

-(void)dealloc {
	[_shareTextView release];
	[_wordLabel release];
	[_attachImage release];
    [_engine release];
    [_initText release];
    [super dealloc];
}

-(void) viewDidLoad{
	[super viewDidLoad];

    _accountLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5,200, 20)];
	_accountLabel.backgroundColor = [UIColor clearColor];
	_accountLabel.textAlignment = UITextAlignmentLeft;
	_accountLabel.font = [UIFont systemFontOfSize:13];
	_accountLabel.textColor = [UIColor colorWithRed:0xc7/255.0f green:0xb8/255.0f blue:0x8a/255.0f alpha:1];
	[self.view addSubview:_accountLabel];
    
    _wordLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 5 , 60, 20)];
	_wordLabel.backgroundColor = [UIColor clearColor];
	_wordLabel.textAlignment = UITextAlignmentRight;
	[self.view addSubview:_wordLabel];
    
    _shareTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 300, 150)];
	_shareTextView.delegate = self;
	_shareTextView.font = [UIFont systemFontOfSize:14];
	[self.view addSubview:_shareTextView];
    _shareTextView.text = _initText;
    
    [self textViewDidChange:_shareTextView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self refreshWithAcctount];
}

-(void) viewDidAppear:(BOOL)animated{
	[super viewDidAppear:animated];
}


-(void)setShareType:(NSString *)shareType {
    [shareType retain];
    [_shareType release];
    _shareType = [shareType retain];
    
    self.title = [NSString stringWithFormat:@"%@分享",_shareType];
    
    [_engine release];
    _engine = [[ShareEngine alloc] initWithType:_shareType];
}


-(void)refreshWithAcctount {
    if (_engine.hasBind) {
        self.zbNavigationItem.rightView = [UIFactory navButtonForTitle:@"提交"];
        _accountLabel.text = [NSString stringWithFormat:@"已绑定账号%@",_engine.account.screenName];
    }else {
        self.zbNavigationItem.rightView = [UIFactory navButtonForTitle:@"绑定账号"];
        _accountLabel.text = @"未绑定账号";
    }
    [(UIButton*)self.zbNavigationItem.rightView  addTarget:self action:@selector(submitButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}

-(void) upload{
	[_engine upload:_shareTextView.text image:_attachImage];
}

-(void) submitButtonPressed:(id)sender{
	[_shareTextView resignFirstResponder];
	if (_engine.hasBind) {
		[self upload];
	}else {
		[_engine bindAccount];
	}
}

-(void) deleteImage{
	self.attachImage = nil;
	[_imageButton removeFromSuperview];
}

-(void) setText:(NSString *) text  image:(UIImage *)image{
    if (_shareTextView) {
        _shareTextView.text = text;
        [self textViewDidChange:_shareTextView];
    } else {
        [text retain];
        [_initText release];
        _initText = text;
    }
	self.attachImage = image;
}


- (void)textViewDidChange:(UITextView *)textView{
	if ([textView.text length] <=MAXWORDS) {
		_wordLabel.textColor = [UIColor colorWithRed:0xc7/255.0f green:0xb8/255.0f blue:0x8a/255.0f alpha:1];
		_wordLabel.font = [UIFont systemFontOfSize:15];
		_wordLabel.text = [NSString stringWithFormat:@"%d",MAXWORDS-[textView.text length]];
	}else {
		_wordLabel.textColor = [UIColor redColor];
		_wordLabel.font = [UIFont boldSystemFontOfSize:15];
		_wordLabel.text = [NSString stringWithFormat:@"%d",[textView.text length]-MAXWORDS];
	}
}

#pragma mark
#pragma mark  WeiboEngineDelegate
-(void)shareEngine:(ShareEngine*)engine bindFinished:(NSError*)err {
    if (_engine.hasBind) {
        [self refreshWithAcctount];
		[self upload];
	}else {
        if (err.code!=kShareErrorCancel) {
            alertMessage(@"认证失败", [err localizedDescription]);
        }
	}
}
-(void)shareEngine:(ShareEngine*)engine uploadFinished:(NSError*)err {
    if (err==nil) {
        [self.navigationController popViewControllerAnimated:YES];
        alertMessage(@"分享成功",nil);
    } else {
        alertMessage(@"分享失败",[err localizedDescription]);    
    }
}

-(void)shareEngine:(ShareEngine*)engine accountInfoUpdated:(NSError*)err {
    [self refreshWithAcctount];    
}

-(void)shareEngine:(ShareEngine *)engine willShowOAuthController:(OAuthController*)controller {
    [controller setNavigationBarBackgroundImage:[IconImage navBarImage]];
    [controller setCancelButton:[UIFactory navButtonForTitle:@"取消"]];
    controller.view.backgroundColor =[UIColor colorWithPatternImage:[IconImage mainBackgroundImage]];
}
@end
